<template>
  <div>
    选择商品
    <!-- 表格 -->
    <div>
      <el-table :data="goodsList" style="width:100%" stripe @selection-change="select">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" label="商品ID" align="center"></el-table-column>
        <el-table-column prop="name" label="商品名称" align="center"></el-table-column>
        <el-table-column prop="stocks" label="商品数量" align="center"></el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div style="text-align:right;margin:15px 15px">
      <el-pagination class="page"
        background
        layout="total, prev, pager, next, jumper"
        :total="count"
        :page-size="10"
        :current-page="page"
        @size-change="changePage"
        @current-change="changePage">
      </el-pagination>
    </div>

    <div style="text-align:center">
      <el-button type="primary" size="small" style="padding:8px 30px;" @click="submit">提 交</el-button>
    </div>
  </div>
</template>

<script>
import { getGoodsList } from '@/api/goods'

export default {
  name: 'SkillSelectGoods',
  data() {
    return {
      page: 1,
      count: 1,
      goodsList: [],

      selectionList: [],
    }
  },
  mounted() {
    this.getGoodsList();
  },
  methods: {
    /**
     * 获取商品列表
     */
    getGoodsList() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        page: this.page,
        limit: 10,
      };
      getGoodsList(params).then(res => {
        if(res.status === 200){
          this.goodsList = res.data;
          this.count = parseInt(res.count);
        }else if(res.status === 204){
          this.goodsList = [];
          this.count = 1;
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 选择商品
     */
    select(selection) {
      this.selectionList = selection;
    },
    /**
     * 提交
     */
    submit() {
      this.$emit('selection',this.selectionList);
    },
    /**
     * 翻页
     */
    changePage(page) {
      this.page = page;
      this.getGoodsList();
    },
  },
}
</script>